﻿using System;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x02000164 RID: 356
public class Card_Data
{
	// Token: 0x0600094D RID: 2381 RVA: 0x00036A30 File Offset: 0x00034C30
	public Card_Data(string file, Texture2D noLoadTex)
	{
		this.file = file;
		this.tex = null;
		this.noLoadTex = noLoadTex;
		this.charaInfo.Load(file);
		this.state = Card_Data.STATE.NO_TEX;
	}

	// Token: 0x17000129 RID: 297
	// (get) Token: 0x0600094E RID: 2382 RVA: 0x00036A6C File Offset: 0x00034C6C
	public bool HasTex
	{
		get
		{
			return this.state == Card_Data.STATE.HAS_TEX;
		}
	}

	// Token: 0x0600094F RID: 2383 RVA: 0x00036A78 File Offset: 0x00034C78
	public void Delete()
	{
		this.buf = null;
		if (this.thread != null && this.thread.IsAlive)
		{
			this.thread.Abort();
		}
		if (this.tex != null)
		{
			UnityEngine.Object.Destroy(this.tex);
			this.tex = null;
		}
	}

	// Token: 0x06000950 RID: 2384 RVA: 0x00036AD8 File Offset: 0x00034CD8
	private void LoadedTex()
	{
		if (this.state == Card_Data.STATE.NO_TEX && this.tex == null && (this.thread == null || !this.thread.IsAlive))
		{
			this.state = Card_Data.STATE.LOADING;
			this.thread = new Thread(new ThreadStart(this.LoadPNG));
			this.thread.Start();
		}
	}

	// Token: 0x06000951 RID: 2385 RVA: 0x00036B48 File Offset: 0x00034D48
	private void LoadPNG()
	{
		this.buf = null;
		FileStream fileStream = new FileStream(this.file, FileMode.Open, FileAccess.Read);
		if (fileStream == null)
		{
			this.state = Card_Data.STATE.NO_TEX;
			return;
		}
		using (BinaryReader binaryReader = new BinaryReader(fileStream))
		{
			try
			{
				this.buf = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
			}
			catch (Exception message)
			{
				GlobalData.Debug_LogWarning(message);
				this.buf = null;
			}
			binaryReader.Close();
		}
		this.state = Card_Data.STATE.LOAD_END;
	}

	// Token: 0x06000952 RID: 2386 RVA: 0x00036BEC File Offset: 0x00034DEC
	private void CreateTex()
	{
		if (this.state == Card_Data.STATE.LOAD_END)
		{
			this.tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
			this.tex.LoadImage(this.buf);
			this.buf = null;
			this.state = Card_Data.STATE.HAS_TEX;
			if ((float)this.tex.width > CardFileList.PNG_Size.x || (float)this.tex.height > CardFileList.PNG_Size.y)
			{
				TextureScale.Bilinear(this.tex, (int)CardFileList.PNG_Size.x, (int)CardFileList.PNG_Size.y);
			}
			this.isChangeTex = true;
		}
	}

	// Token: 0x06000953 RID: 2387 RVA: 0x00036C94 File Offset: 0x00034E94
	private void ForcedLoadPNG()
	{
		if (this.thread != null && this.thread.IsAlive)
		{
			this.thread.Abort();
		}
		this.buf = null;
		FileStream fileStream = new FileStream(this.file, FileMode.Open, FileAccess.Read);
		if (fileStream == null)
		{
			this.state = Card_Data.STATE.NO_TEX;
			return;
		}
		using (BinaryReader binaryReader = new BinaryReader(fileStream))
		{
			try
			{
				this.buf = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
			}
			catch (Exception message)
			{
				GlobalData.Debug_LogWarning(message);
				this.buf = null;
			}
			binaryReader.Close();
		}
		this.tex = new Texture2D(1, 1);
		this.tex.LoadImage(this.buf);
		if ((float)this.tex.width > CardFileList.PNG_Size.x || (float)this.tex.height > CardFileList.PNG_Size.y)
		{
			TextureScale.Bilinear(this.tex, (int)CardFileList.PNG_Size.x, (int)CardFileList.PNG_Size.y);
		}
		this.buf = null;
		this.state = Card_Data.STATE.HAS_TEX;
		this.isChangeTex = true;
	}

	// Token: 0x06000954 RID: 2388 RVA: 0x00036DE0 File Offset: 0x00034FE0
	private void ReleaseTex()
	{
		if (this.state == Card_Data.STATE.NO_TEX || this.isSelect)
		{
			return;
		}
		this.state = Card_Data.STATE.NO_TEX;
		this.buf = null;
		if (this.thread != null && this.thread.IsAlive)
		{
			this.thread.Abort();
		}
		if (this.tex != null)
		{
			UnityEngine.Object.Destroy(this.tex);
			this.tex = null;
			this.isChangeTex = true;
		}
	}

	// Token: 0x06000955 RID: 2389 RVA: 0x00036E64 File Offset: 0x00035064
	public Texture2D Texture(bool forcedLoad = false)
	{
		if (this.tex != null)
		{
			return this.tex;
		}
		if (forcedLoad)
		{
			this.ForcedLoadPNG();
			return this.tex;
		}
		return this.noLoadTex;
	}

	// Token: 0x06000956 RID: 2390 RVA: 0x00036E98 File Offset: 0x00035098
	public void UpdateSprite(Image image)
	{
		if (this.show || this.isSelect)
		{
			this.LoadedTex();
		}
		else
		{
			this.ReleaseTex();
		}
		if (this.state == Card_Data.STATE.LOAD_END)
		{
			this.CreateTex();
		}
		if (this.isChangeTex)
		{
			Texture2D texture2D = this.Texture(false);
			Vector2 png_Size = CardFileList.PNG_Size;
			if (texture2D != null)
			{
				png_Size.x = (float)texture2D.width;
				png_Size.y = (float)texture2D.height;
			}
			Rect rect = new Rect(Vector2.zero, png_Size);
			Vector2 pivot = png_Size * 0.5f;
			image.sprite = Sprite.Create(this.Texture(false), rect, pivot, 100f, 0U, SpriteMeshType.FullRect);
			this.isChangeTex = false;
		}
	}

	// Token: 0x06000957 RID: 2391 RVA: 0x00036F5C File Offset: 0x0003515C
	public void ChangeTex()
	{
		this.isChangeTex = true;
	}

	// Token: 0x0400090C RID: 2316
	public string file;

	// Token: 0x0400090D RID: 2317
	public byte[] buf;

	// Token: 0x0400090E RID: 2318
	public CharaInfo charaInfo = new CharaInfo();

	// Token: 0x0400090F RID: 2319
	public bool show;

	// Token: 0x04000910 RID: 2320
	public bool isSelect;

	// Token: 0x04000911 RID: 2321
	private bool isChangeTex;

	// Token: 0x04000912 RID: 2322
	private Texture2D tex;

	// Token: 0x04000913 RID: 2323
	private Texture2D noLoadTex;

	// Token: 0x04000914 RID: 2324
	private Thread thread;

	// Token: 0x04000915 RID: 2325
	private Card_Data.STATE state;

	// Token: 0x02000165 RID: 357
	private enum STATE
	{
		// Token: 0x04000917 RID: 2327
		NO_TEX,
		// Token: 0x04000918 RID: 2328
		LOADING,
		// Token: 0x04000919 RID: 2329
		LOAD_END,
		// Token: 0x0400091A RID: 2330
		HAS_TEX
	}
}
